package com.gouying.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gouying.common.utils.PageUtils;
import com.gouying.common.utils.Query;
import com.gouying.web.dao.PurchaseDetailDao;
import com.gouying.web.entity.PurchaseDetailEntity;
import com.gouying.web.service.PurchaseDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 采购需求查询
 */

@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();

        //搜索框输入的关键字
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            //进行模糊查询
            queryWrapper.and(w->{
               w.eq("purchase_id",key).or().eq("sku_id",key);
            });
        }

        //采购需求状态
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status",status);
        }

        //仓库号
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            queryWrapper.eq("ware_id",wareId);
        }

        //分页查询
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }

    //根据每个采购单id查询采购单所有信息
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> purchaseId = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        return purchaseId;
    }

}